home *** CD-ROM | disk | FTP | other *** search
/ Total Web Page (Professional Suite) / Total Web Page 99.iso / CGI / DOWNLOAD.CGI-S=SEARCH&C=TXT&F=SEARCH.PL < prev    next >
Perl Script  |  1996-06-03  |  6KB  |  187 lines

  1. #!/usr/bin/perl
  2. ##############################################################################
  3. # Simple Search                 Version 1.0                                  #
  4. # Copyright 1996 Matt Wright    mattw@worldwidemart.com                      #
  5. # Created 12/16/95              Last Modified 12/16/95                       #
  6. # Scripts Archive at:           http://www.worldwidemart.com/scripts/        #
  7. ##############################################################################
  8. # COPYRIGHT NOTICE                                                           #
  9. # Copyright 1996 Matthew M. Wright  All Rights Reserved.                     #
  10. #                                                                            #
  11. # Simple Search may be used and modified free of charge by anyone so long as #
  12. # this copyright notice and the comments above remain intact.  By using this #
  13. # code you agree to indemnify Matthew M. Wright from any liability that      #  
  14. # might arise from it's use.                                                 #  
  15. #                                                                            #
  16. # Selling the code for this program without prior written consent is         #
  17. # expressly forbidden.  In other words, please ask first before you try and  #
  18. # make money off of my program.                                              #
  19. #                                                                            #
  20. # Obtain permission before redistributing this software over the Internet or #
  21. # in any other medium.  In all cases copyright and header must remain intact.#
  22. ##############################################################################
  23. # Define Variables                                 #
  24.  
  25. $basedir = '/mnt/web/guide/worldwidemart/scripts/';
  26. $baseurl = 'http://worldwidemart.com/scripts/';
  27. @files = ('*.shtml','demos/links/*.html','demos/guest/*.html');
  28. $title = "Matt's Script Archive";
  29. $title_url = 'http://worldwidemart.com/scripts/';
  30. $search_url = 'http://worldwidemart.com/scripts/demos/search/search.html';
  31.  
  32. # Done                                         #
  33. ##############################################################################
  34.  
  35. # Parse Form Search Information
  36. &parse_form;
  37.  
  38. # Get Files To Search Through
  39. &get_files;
  40.  
  41. # Search the files
  42. &search;
  43.  
  44. # Print Results of Search
  45. &return_html;
  46.  
  47.  
  48. sub parse_form {
  49.  
  50.    # Get the input
  51.    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  52.  
  53.    # Split the name-value pairs
  54.    @pairs = split(/&/, $buffer);
  55.  
  56.    foreach $pair (@pairs) {
  57.       ($name, $value) = split(/=/, $pair);
  58.  
  59.       $value =~ tr/+/ /;
  60.       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  61.  
  62.       $FORM{$name} = $value;
  63.    }
  64. }
  65.  
  66. sub get_files {
  67.  
  68.    chdir($basedir);
  69.    foreach $file (@files) {
  70.       $ls = `ls $file`;
  71.       @ls = split(/\s+/,$ls);
  72.       foreach $temp_file (@ls) {
  73.          if (-d $file) {
  74.             $filename = "$file$temp_file";
  75.             if (-T $filename) {
  76.                push(@FILES,$filename);
  77.             }
  78.          }
  79.          elsif (-T $temp_file) {
  80.             push(@FILES,$temp_file);
  81.          }
  82.       }
  83.    }
  84. }
  85.  
  86. sub search {
  87.  
  88.    @terms = split(/\s+/, $FORM{'terms'});
  89.  
  90.    foreach $FILE (@FILES) {
  91.  
  92.       open(FILE,"$FILE");
  93.       @LINES = <FILE>;
  94.       close(FILE);
  95.  
  96.       $string = join(' ',@LINES);
  97.       $string =~ s/\n//g;
  98.       if ($FORM{'boolean'} eq 'AND') {
  99.          foreach $term (@terms) {
  100.             if ($FORM{'case'} eq 'Insensitive') {
  101.                if (!($string =~ /$term/i)) {
  102.                   $include{$FILE} = 'no';
  103.             last;
  104.                }
  105.                else {
  106.                   $include{$FILE} = 'yes';
  107.                }
  108.             }
  109.             elsif ($FORM{'case'} eq 'Sensitive') {
  110.                if (!($string =~ /$term/)) {
  111.                   $include{$FILE} = 'no';
  112.                   last;
  113.                }
  114.                else {
  115.                   $include{$FILE} = 'yes';
  116.                }
  117.             }
  118.          }
  119.       }
  120.       elsif ($FORM{'boolean'} eq 'OR') {
  121.          foreach $term (@terms) {
  122.             if ($FORM{'case'} eq 'Insensitive') {
  123.                if ($string =~ /$term/i) {
  124.                   $include{$FILE} = 'yes';
  125.                   last;
  126.                }
  127.                else {
  128.                   $include{$FILE} = 'no';
  129.                }
  130.             }
  131.             elsif ($FORM{'case'} eq 'Sensitive') {
  132.                if ($string =~ /$term/) {
  133.           $include{$FILE} = 'yes';
  134.                   last;
  135.                }
  136.                else {
  137.                   $include{$FILE} = 'no';
  138.                }
  139.             }
  140.          }
  141.       }
  142.       if ($string =~ /<title>(.*)<\/title>/i) {
  143.          $titles{$FILE} = "$1";
  144.       }
  145.       else {
  146.          $titles{$FILE} = "$FILE";
  147.       }
  148.    }
  149. }
  150.       
  151. sub return_html {
  152.    print "Content-type: text/html\n\n";
  153.    print "<html>\n <head>\n  <title>Results of Search</title>\n </head>\n";
  154.    print "<body>\n <center>\n  <h1>Results of Search in $title</h1>\n </center>\n";
  155.    print "Below are the results of your Search in no particular order:<p><hr size=7 width=75%><p>\n";
  156.    print "<ul>\n";
  157.    foreach $key (keys %include) {
  158.       if ($include{$key} eq 'yes') {
  159.          print "<li><a href=\"$baseurl$key\">$titles{$key}</a>\n";
  160.       }
  161.    }
  162.    print "</ul>\n";
  163.    print "<hr size=7 width=75%>\n";
  164.    print "Search Information:<p>\n";
  165.    print "<ul>\n";
  166.    print "<li><b>Terms:</b> ";
  167.    $i = 0;
  168.    foreach $term (@terms) {
  169.       print "$term";
  170.       $i++;
  171.       if (!($i == @terms)) {
  172.          print ", ";
  173.       }
  174.    }
  175.    print "\n";
  176.    print "<li><b>Boolean Used:</b> $FORM{'boolean'}\n";
  177.    print "<li><b>Case $FORM{'case'}</b>\n";
  178.    print "</ul><br><hr size=7 width=75%><P>\n";
  179.    print "<ul>\n<li><a href=\"$search_url\">Back to Search Page</a>\n";
  180.    print "<li><a href=\"$title_url\">$title</a>\n";
  181.    print "</ul>\n";
  182.    print "<hr size=7 width=75%>\n";
  183.    print "Search Script written by Matt Wright and can be found at <a href=\"http://www.worldwidemart.com/scripts/\">Matt's Script Archive</a>\n";
  184.    print "</body>\n</html>\n";
  185. }
  186.    
  187.